﻿/// <reference path="../Controls/BaseComplexControl.js" />

var $ValidatorDisplay = TA.Turbo.Controls.ValidatorDisplay =
{
	None: 0,
	Static: 1,
	Dynamic: 2
};


TA.Turbo.Controls.BaseValidator = function BaseValidator(clientId)
{
	this.BaseComplexControl(clientId);

}
TA.Turbo.Controls.BaseValidator.Inherit(TA.Turbo.Controls.BaseComplexControl);
var ____a;
if (____a)
{
	TA.Turbo.Controls.BaseValidator.prototype = new TA.Turbo.Controls.BaseComplexControl();
	TA.Turbo.Controls.BaseValidator.prototype.BaseComplexControl = new TA.Turbo.Controls.BaseComplexControl();
}
var $BaseValidator = TA.Turbo.Controls.BaseValidator;

$BaseValidator.prototype._lastValue = "---UNKNOWN VALUE---";
$BaseValidator.prototype._isValid = false;
$BaseValidator.prototype._validatedControl = null;

$PropertyDefinition.Define($BaseValidator, "Group", "G", "");
$PropertyDefinition.Define($BaseValidator, "ValidatedControlId", "VC", "",
{
	OnChanged: function()
	{
		this._validatedControl = null;
	}
});
$PropertyDefinition.Define($BaseValidator, "InlineMessage", "IM", "*",
{
	OnChanged: function()
	{
		this._control.innerHTML = this.get_InlineMessage();
	}
});
$PropertyDefinition.Define($BaseValidator, "SummaryMessage", "SM", "Validation failed.");
$PropertyDefinition.Define($BaseValidator, "Display", "VD", $ValidatorDisplay.Static);
$PropertyDefinition.Define($BaseValidator, "ValidateEmpty", "VE", false);
$PropertyDefinition.Define($BaseValidator, "IsValid", "IV", true,
{
	Getter: function()
	{
		this._UpdateValidStatus();
		return this._isValid;
	},
	Setter: function(value)
	{
		this._lastValue = this.get_ValidatedValue();
		this._isValid = value;
		this._OnIsValidChanged();
	},
	OnChanged: function()
	{
		switch (this.get_Display())
		{
			case $ValidatorDisplay.Dynamic:
				this.get_Control().style["display"] = this._isValid ? "none" : "";
				break;
			case $ValidatorDisplay.Static:
				this.get_Control().style["visibility"] = this._isValid ? "hidden" : "";
				break;
			case $ValidatorDisplay.None:
				this.get_Control().style["display"] = "none";
				break;
		}
	}
});

$BaseValidator.prototype.get_ValidatedControl = function get_ValidatedControl()
{
	if (this._validatedControl == null)
		this._validatedControl = $(this.get_ValidatedControlId());
	return this._validatedControl;
}

$BaseValidator.prototype.get_ValidatedValue = function get_ValidatedValue()
{
	return this.get_ValidatedControl().get_Object().get_Value();
}

$BaseValidator.prototype.Load = function Load()
{
	this.BaseComplexControl.Load.call(this);

	if (this.get_ValidatedControl() != null)
	{
		this.get_ValidatedControl().get_Object().AddEvent("ValueChanged", this._ValidatedControl_ValueChanged, this);
	}
}
$BaseValidator.prototype._OnValidate = function _OnValidate()
{
}
$BaseValidator.prototype._OnEnabledChanged = function _OnEnabledChanged()
{
	this.BaseComplexControl._OnEnabledChanged.call(this);

	this._lastValue = "---UNKNOWN VALUE---";
}
$BaseValidator.prototype._UpdateValidStatus = function _UpdateValidStatus()
{
	if (this.get_ValidatedControl() == null)
		this.set_IsValid(true);
	else if (this.get_ValidatedValue() != this._lastValue)
	{
		if (!this.get_Enabled() || !this.get_Shown() || ((this.get_ValidatedValue() == null || this.get_ValidatedValue() == "") && !this.get_ValidateEmpty()))
			this.set_IsValid(true);
		else
			this._OnValidate();
	}
}
$BaseValidator.prototype._ValidatedControl_ValueChanged = function _ValidatedControl_ValueChanged()
{
	this._UpdateValidStatus();
}

